home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Purity / Purity #48 (1995-06-25)(PackMAN)(DE)[WB].zip / Purity #48 (1995-06-25)(PackMAN)(DE)[WB].adf / BigDivision / BigDivision.pas < prev    next >
Pascal/Delphi Source File  |  1995-06-23  |  4KB  |  154 lines

  1. PROGRAM BigDivision;
  2.  
  3. (* This program was created to show you how to program nicely.
  4.    I have found the code in an old Basic Program, which I converted to
  5.    Pascal to make it clearer. If you want to contact me, please write
  6.    to Daniel Amor <daniel.amor@student.uni-tuebingen.de> *)
  7.  
  8. USES CRT;
  9.  
  10. (* The Unit CRT opens a window for us and gives us some fancy commands
  11.    like ReadKey, WaitForKey etc. Look in your manual for further information *)
  12.  
  13. VAR Dividend, Divisor: LONGINT;
  14.     Laenge:            LONGINT;
  15.     what:              CHAR;
  16.     Text:              ARRAY [1..8] OF STRING[35];
  17.  
  18. (* These variables are needed in the main program, actually only the numbers.
  19.    The other are only here to make the program more comfortable *)
  20.  
  21. PROCEDURE Division(Dividend, Divisor: LONGINT; Laenge: LONGINT);
  22.  
  23. (* This is the division sub-routine, if you have a close look you will find
  24.    out, that it works the way you would divide two numbers on a piece of 
  25.    paper, like this it is very exact *)
  26.  
  27. VAR i,stellen: INTEGER;
  28.     Komma    : BOOLEAN;
  29.  
  30. BEGIN
  31.   Komma:=FALSE;
  32.  
  33.   IF ((Dividend<0) AND (Divisor>=0)) OR ((Dividend>=0) AND (Divisor<0)) THEN 
  34.     WRITE("-");
  35.  
  36.   (* In case of a negative value, we have to add a minus symbol *)
  37.  
  38.   IF Dividend<0 THEN Dividend := Dividend*(-1);
  39.   IF Divisor<0  THEN Divisor  := Divisor*(-1);
  40.  
  41.   (* Now we make them positive to easen the calculation *)
  42.  
  43.   FOR i:=1 TO Laenge DO 
  44.   BEGIN
  45.     Stellen:=TRUNC(Dividend/Divisor);
  46.     WRITE(Stellen);
  47.     Dividend:=10*(Dividend-(Stellen*Divisor));
  48.     IF (NOT Komma) AND (i<Laenge) THEN 
  49.     BEGIN 
  50.       WRITE(".");
  51.       Komma:=TRUE;
  52.     END;
  53.   END;
  54.  
  55.   (* This is the main loop, which works just as if you would do it by hand.
  56.      Try it out yourself *)
  57.  
  58. END;
  59.  
  60. BEGIN
  61.   WindowTitles("BigDivision V1.0","Created by Daniel Amor in 1995. Public Domain!");
  62.   REPEAT
  63.     REPEAT
  64.       WRITE('(E)nglish or/oder (D)eutsch? ');
  65.       what:=ReadKey;
  66.       WRITELN;
  67.     UNTIL (UPCASE(what)="E") or (UPCASE(what)="D");
  68.  
  69.     (* This program is very picky about correct inputs, so if you do enter
  70.        a wrong key, you have another try *)
  71.  
  72.     WRITELN;
  73.     IF UPCASE(what)="E" THEN
  74.     BEGIN
  75.       text[1]:="Welcome to BigDivision!";
  76.       text[2]:="Dividend: ";
  77.       text[3]:="Divisor: ";
  78.       text[4]:="Length: ";
  79.       text[5]:="Are you sure (y/n)? ";
  80.       text[6]:="Y";
  81.       text[7]:="N";
  82.       text[8]:="Again? (y/n)";
  83.     END
  84.     ELSE
  85.     BEGIN
  86.       text[1]:="Willkommen zu BigDivision!";
  87.       text[2]:="Dividend: ";
  88.       text[3]:="Divisor: ";
  89.       text[4]:="Länge: ";
  90.       text[5]:="Sind Sie sicher (j/n)? ";
  91.       text[6]:="J";
  92.       text[7]:="N";
  93.       text[8]:="Nochmal? (j/n)";
  94.     END;
  95.  
  96.     (* Here is the text assigned to the strings, depending on your input,
  97.        either English or German *)
  98.  
  99.     WRITELN(text[1]);
  100.     REPEAT
  101.  
  102.       REPEAT
  103.         WRITE(text[2]);
  104.         READLN(Dividend);
  105.       UNTIL Dividend<>0;
  106.  
  107.       (* You have to enter a value, which is not zero *)
  108.  
  109.       REPEAT
  110.         WRITE(text[3]);
  111.         READLN(Divisor);
  112.       UNTIL Dividend<>0;
  113.  
  114.       (* You have to enter a value, which is not zero *)
  115.  
  116.       REPEAT
  117.         WRITE(text[4]);
  118.         READLN(Laenge);
  119.       UNTIL (Laenge>0) AND (Laenge<32000);
  120.  
  121.       (* You have to enter a value, which is between 0 and 32000 *)
  122.  
  123.       WRITE(text[5]);
  124.       what:=ReadKey;
  125.       WRITELN;
  126.  
  127.       (* Are your inputs correct? If yes then we leave the REPEAT-loop *)
  128.  
  129.     UNTIL UPCASE(what)=text[6];
  130.     WRITELN;
  131.     WRITE(Dividend," : ",Divisor," = ")
  132.  
  133.     (* This outputs the entered values *)
  134.  
  135.     Division(Dividend,Divisor,Laenge);
  136.  
  137.     (* Now we jump into the sub-routine to calculate the division *) 
  138.  
  139.     WRITELN;
  140.     WRITELN;
  141.     WRITE(Text[8]);
  142.     what:=ReadKey;
  143.     WRITELN;
  144.     WRITELN;
  145.  
  146.     (* Only some output onto the screen to make the user happy *)
  147.  
  148.   UNTIL UPCASE(what)=Text[7];
  149.  
  150.     (* We repeat this until you have enough of it *)
  151.  
  152. END.
  153.  
  154.